package com.shop.controller;

import com.shop.domain.ContractProduct;
import com.shop.domain.Factory;
import com.shop.service.FactoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * FactoryController
 * Created by heqianqian on 2017/7/10.
 */
@Controller
@RequestMapping("/factory")
public class FactoryController extends BaseController {

    @Resource
    private FactoryService<Factory> factoryService;

    /**
     * 查询列表
     */
    @RequestMapping("/list")
    public String list(Model model) {
        List<Factory> list = factoryService.find(null);
        model.addAttribute("list", list);
        return "factory/factory_list";//转向页面
    }

    /**
     * 跳转到添加页面
     */
    @RequestMapping("/create")
    public String create() {
        return "factory/factory_create";
    }

    /**
     * 插入元素
     */
    @RequestMapping("/insert")
    public String insert(Factory factory) {
        factoryService.insert(factory);
        return "redirect:/factory/list";
    }

    /**
     * 跳转到修改页面
     */
    @RequestMapping("/modify")
    public String modify(Long id, Model model) {
        Factory factory = factoryService.get(id);
        model.addAttribute("obj", factory);
        return "factory/factory_update";
    }

    /**
     * 修改元素
     */
    @RequestMapping("/update")
    public String update(Factory factory) {
        factoryService.update(factory);
        return "redirect:/factory/list";
    }


    /**
     * 按照id删除元素
     */
    @RequestMapping("/deleteById")
    public String deleteById(Long id) {
        factoryService.deleteById(id);
        return "redirect:/factory/list";
    }

    /**
     * 按照id删除元素
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam("id") Long[] ids) {
        factoryService.delete(ids);
        return "redirect:/factory/list";
    }

    /**
     * 查看商品详细信息
     */
    @RequestMapping("/detail")
    public String detail(Long id, Model model) {
        Factory factory = factoryService.get(id);
        model.addAttribute("obj", factory);
        return "factory/factory_view";
    }

    /**
     * 商品上架
     */
    @RequestMapping("/start")
    public String start(@RequestParam("id") Long[] ids) {
        factoryService.start(ids);
        return "redirect:/factory/list";
    }

    /**
     * 商品下架
     */
    @RequestMapping("/stop")
    public String stop(@RequestParam("id") Long[] ids) {
        factoryService.stop(ids);
        return "redirect:/factory/list";
    }
}


